/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Riso-Desktop
 */
@WebServlet(name = "skore", urlPatterns = {"/skore"})
public class skore extends HttpServlet {

    PrintWriter out;
    HttpSession session;
    int id;
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

    @Override
    public void init() {
        pripojenie();
    }

    private void pripojenie() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "portalpredeti", "root", "");
        } catch (Exception ex) {
        }
    }

    private ResultSet nacitajvysledky() {
        ResultSet rss = null;
        try {
            stmt = con.createStatement();
            rss = stmt.executeQuery("Select typ_ulohy, body from vysledky where id_ziaka = " + id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rss;

    }

    private int ziskaneBody(String s) {
        int cislo = -1;
        cislo = Integer.parseInt(s.substring(0, s.indexOf("/")));
        return cislo;
    }

    private int maxBody(String s) {
        int cislo = -1;
        cislo = Integer.parseInt(s.substring(s.indexOf("/") + 1));
        return cislo;
    }

    private void ukazSkore() {
        int[][] body;
        int[][] vypocitane = new int[4][2];
        try {
            rs = nacitajvysledky();
            rs.last();
            body = new int[rs.getRow()][3];
            rs.beforeFirst();
            int i = 0;
            while (rs.next()) {
                body[i][0] = rs.getInt("typ_ulohy");
                body[i][1] = ziskaneBody(rs.getString("body"));
                body[i][2] = maxBody(rs.getString("body"));
                i++;
            }
            for (int j = 0; j < body.length; j++) {
                if (body[j][0] == 2) {
                    vypocitane[0][0] = vypocitane[0][0] + body[j][1];
                    vypocitane[0][1] = vypocitane[0][1] + body[j][2];
                } else if (body[j][0] == 3) {
                    vypocitane[1][0] = vypocitane[1][0] + body[j][1];
                    vypocitane[1][1] = vypocitane[1][1] + body[j][2];
                } else if (body[j][0] == 4) {
                    vypocitane[2][0] = vypocitane[2][0] + body[j][1];
                    vypocitane[2][1] = vypocitane[2][1] + body[j][2];
                } else if (body[j][0] == 5) {
                    vypocitane[3][0] = vypocitane[3][0] + body[j][1];
                    vypocitane[3][1] = vypocitane[3][1] + body[j][2];
                }
            }
            out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
                    + "<html xmlns='http://www.w3.org/1999/xhtml'>"
                    + "<head>"
                    + "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"
                    + "<title>Untitled Document</title>"
                    + "</head>"
                    + "<body style='background-image:url(lib/bart-marks.png);background-repeat:no-repeat;background-position:center'>"
                    + "<div align='center' style='margin-top:100px;background-image:url(lib/pozadie_skore.png);background-repeat:no-repeat;background-position:center;height:88px'>"
                    + "<div style='padding-top:8px'>"
                    + "Prvá úloha: " + vypocitane[0][0] + " bodov z " + vypocitane[0][1] + "<br/>"
                    + "Druhá úloha: " + vypocitane[1][0] + " bodov z " + vypocitane[1][1] + "<br/>"
                    + "Tretia úloha: " + vypocitane[2][0] + " bodov z " + vypocitane[2][1] + "<br/>"
                    + "Štvrtá úloha: " + vypocitane[3][0] + " bodov z " + vypocitane[3][1] + "<br/>"
                    + "</div>"
                    + "<br/><a style='color:#000' href='servlet'>Naspeť na ponuku</a>"
                    + "</div>"
                    + "</body>"
                    + "</html>");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        session = request.getSession();
        try {
            id = (Integer) session.getAttribute("ID");
            if (id > 0) {
                ukazSkore();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
